package main

import (
	"flag"
	"fmt"
	"os"
	"strings"

	"github.com/bzsome/ChaoGom3u8/chaoM3u8"
	"github.com/bzsome/ChaoGom3u8/utils"
	"github.com/dustin/go-humanize"
)

func main() {
	var url string
	var path string
	var wpSize int
	var PoolSize int
	var ChuckSize int64

	// StringVar用指定的名称、控制台参数项目、默认值、使用信息注册一个string类型flag，并将flag的值保存到p指向的变量
	flag.StringVar(&url, "url", "", "url地址, 必填")
	flag.StringVar(&path, "path", "downloads", "保存路径")
	flag.IntVar(&wpSize, "w", 5, "同时下载文件数量,默认为5")
	flag.IntVar(&PoolSize, "dn", 10, "单文件下载并行数量,默认为10")
	flag.Int64Var(&ChuckSize, "dc", 1024*100, "下载数据块大小，默认100K")
	// 从arguments中解析注册的flag。必须在所有flag都注册好而未访问其值时执行。未注册却使用flag -help时，会返回ErrHelp。
	flag.Parse()

	if !strings.HasPrefix(url, "http") {
		fmt.Println("cmd参数错误，url不能为空，", url)
		return
	}

	m3u8Down := chaoM3u8.M3u8Down{
		Url:       url,
		SaveDir:   path,
		WpSize:    wpSize,
		PoolSize:  PoolSize,
		ChuckSize: ChuckSize,
	}

	countTime := utils.New()
	m3u8Down.Down()
	exeTime := countTime.EndAndGetExeTime()
	fileInfo, _ := os.Stat(m3u8Down.GetSavePath())
	//文件大小
	fileSize := fileInfo.Size()
	fmt.Printf("%s 下载用时：%.2f 秒，保存路径：%s", humanize.Bytes(uint64(fileSize)), exeTime.Seconds(), m3u8Down.GetSavePath())
}
